<?php

namespace app\adminabc\controller;

use Think\Controller;
use app\adminabc\controller\Base;

class Role extends Base {
	public function index() {
		$cid = $this->request->param ( 'id' );
		$list = model ( 'Role' )->getRole ();
		$this->assign ( 'list', $list );
		return $this->fetch ( 'list-role' );
	}
	function add() {
		if ($this->request->isPost ()) {
			$data ['name'] = $this->request->param ( 'title' );
			$data ['status'] = $this->request->param ( 'status' );
			$data ['pid'] = 0;
			$data ['pid'] = 0;
			$data ['create_time'] = time ();
			$data ['update_time'] = time ();
			$node = explode ( ',', $this->request->param ( 'nid' ) );
			
			$log = model ( 'Role' )->add ( $data );
			model ( 'Node' )->delAccess ( array ([ 'role_id','=',$log ] ) );
			foreach ( $node as $k => $v ) {
				if ($v > 0) {
					unset ( $data );
					$nodeInfo = model ( 'Node' )->nodeInfo ( array ([ 'id','=',$v ] ) );
					$data ['role_id'] = $log;
					$data ['node_id'] = $nodeInfo ['id'];
					$data ['level'] = $nodeInfo ['level'];
					$data ['pid'] = $nodeInfo ['pid'];
					model ( 'Node' )->addAccess ( $data );
				}
			}
			$this->success ( '添加成功' );
		} else {
			$node = model ( 'Role' )->tree ();
			foreach ( $node as $k => $v ) {
				$zNodes = sprintf ( "{ id:%s, pId:%s, name:\"%s\", open:true},", $v ['id'], $v ['pid'], $v ['title'] );
				foreach ( $v ['item'] as $kk => $vv ) {
					$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\", open:true},", $vv ['id'], $v ['id'], $vv ['title'] );
					foreach ( $vv ['item'] as $kkk => $vvv )
						$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\"},", $vvv ['id'], $vv ['id'], $vvv ['title'] );
				}
			}
			
			$this->assign ( 'zNodes', $zNodes );
			return $this->fetch ( '' );
		}
	}
	function edit() {
		$id = $this->request->param ( 'id' );
		$roleInfo = model ( 'Role' )->roleInfo ( array ([ 'id','=',$id ] ) );
		if ($this->request->isPost ()) {
			$data ['name'] = $this->request->param ( 'title' );
			$data ['status'] = $this->request->param ( 'status' );
			$data ['pid'] = 0;
			$data ['pid'] = 0;
			$data ['update_time'] = time ();
			$node = explode ( ',', $this->request->param ( 'nid' ) );
			$log = model ( 'Role' )->edit ( $id, $data );
			if ($this->request->param ( 'nid' ) != '') {
				model ( 'Node' )->delAccess ( array ([ 'role_id','=',$id ] ) );
				foreach ( $node as $k => $v ) {
					if ($v > 0) {
						unset ( $data );
						$nodeInfo = model ( 'Node' )->nodeInfo ( array ([ 'id','=',$v ] ) );
						$data ['role_id'] = $id;
						$data ['node_id'] = $nodeInfo ['id'];
						$data ['level'] = $nodeInfo ['level'];
						$data ['pid'] = $nodeInfo ['pid'];
						model ( 'Node' )->addAccess ( $data );
					}
				}
			}
			$this->success ( '修改成功' );
		} else {
			$node = model ( 'Role' )->tree ();
			foreach ( $node as $k => $v ) {
				$AccessInfo = model ( 'Node' )->AccessInfo ( array ([ 'role_id','=',$id ],[ 'node_id','=',$v ['id'] ] ) );
				if ($AccessInfo ['id'] > 0)
					$zNodes = sprintf ( "{ id:%s, pId:%s, name:\"%s\",  checked:true, open:true},", $v ['id'], $v ['pid'], $v ['title'] );
				else
					$zNodes = sprintf ( "{ id:%s, pId:%s, name:\"%s\",    open:true},", $v ['id'], $v ['pid'], $v ['title'] );
				foreach ( $v ['item'] as $kk => $vv ) {
					$AccessInfo = model ( 'Node' )->AccessInfo ( array ([ 'role_id','=',$id ],[ 'node_id','=',$vv ['id'] ] ) );
					if ($AccessInfo ['id'] > 0)
						$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\", checked:true, open:true},", $vv ['id'], $v ['id'], $vv ['title'] );
					else
						$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\", open:true},", $vv ['id'], $v ['id'], $vv ['title'] );
					foreach ( $vv ['item'] as $kkk => $vvv ) {
						$AccessInfo = model ( 'Node' )->AccessInfo ( array ([ 'role_id','=',$id ],[ 'node_id','=',$vvv ['id'] ] ) );
						if ($AccessInfo ['id'] > 0)
							$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\", checked:true },", $vvv ['id'], $vv ['id'], $vvv ['title'] );
						else
							$zNodes .= sprintf ( "{ id:%s, pId:%s, name:\"%s\"},", $vvv ['id'], $vv ['id'], $vvv ['title'] );
					}
				}
			}
			$this->assign ( 'roleInfo', $roleInfo );
			$this->assign ( 'zNodes', $zNodes );
			return $this->fetch ( 'add' );
		}
	}
	function del() {
		$id = $this->request->param ( 'id' );
		$log1 = model ( 'Role' )->del ( array ([ 'id','=',$id ] ) );
		if ($log1 > 0) {
			$log2 = model ( 'Node' )->delAccess ( array ([ 'role_id','=',$id ] ) );
			echo json_encode ( array ('status' => 'yes','msg' => '删除成功' ) );
		} else
			echo json_encode ( array ('status' => 'no','msg' => '删除失败' ) );
	}
}